package cn.ibizlab.odoo.core.odoo_payment.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ipayment_transaction;
import cn.ibizlab.odoo.core.odoo_payment.domain.Payment_transaction;
import cn.ibizlab.odoo.core.odoo_payment.filter.Payment_transactionSearchContext;
import cn.ibizlab.odoo.core.odoo_payment.service.IPayment_transactionService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[付款交易] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Payment_transactionServiceImpl implements IPayment_transactionService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ipayment_transactionClientService payment_transactionClientService;

    @Override
    public boolean create(Payment_transaction payment_transaction) {
        boolean bOk = false ;
        Ipayment_transaction clientModel = this.convert2Model(payment_transaction,null);
        payment_transactionClientService.create(clientModel) ;
        this.convert2Domain(clientModel,payment_transaction);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //payment_transactionClientService.removeBatch(payment_transactions) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ipayment_transaction clientModel = payment_transactionClientService.createModel();
        clientModel.setId(id);
        payment_transactionClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Payment_transaction> payment_transactions, int batchSize) {

                        //payment_transactionClientService.createBatch(payment_transactions,batchSize) ;

    }

    @Override
    public Payment_transaction get(Integer id) {
        Ipayment_transaction clientModel = payment_transactionClientService.createModel();
        clientModel.setId(id);
        payment_transactionClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Payment_transaction get(Payment_transaction payment_transaction) {
        Payment_transaction retpayment_transaction = this.get(payment_transaction.getId()) ;
    	BeanCopier copier=BeanCopier.create(retpayment_transaction.getClass(), payment_transaction.getClass(), false);
        copier.copy(retpayment_transaction, payment_transaction, null);
        return payment_transaction ;
    }

    @Override
    public void updateBatch(List<Payment_transaction> payment_transactions, int batchSize) {

        //payment_transactionClientService.updateBatch(payment_transactions,batchSize) ;

    }

    @Override
    public boolean update(Payment_transaction payment_transaction) {
        boolean bOk = false ;
        Ipayment_transaction clientModel = this.convert2Model(payment_transaction,null);
        payment_transactionClientService.update(clientModel) ;
        this.convert2Domain(clientModel,payment_transaction) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Payment_transaction> searchDefault(Payment_transactionSearchContext context) {
        List<Payment_transaction> list = new ArrayList<Payment_transaction>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_transaction> clientModelList = payment_transactionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_transaction clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_transaction>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据付款查询
     */
    @Override
    public Page<Payment_transaction> selectByOdoo_payment(Integer id){
        Payment_transactionSearchContext context = new Payment_transactionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_transaction> list = new ArrayList<Payment_transaction>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_transaction> clientModelList = payment_transactionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_transaction clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_transaction>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据付款收单方查询
     */
    @Override
    public Page<Payment_transaction> selectByOdoo_acquirer(Integer id){
        Payment_transactionSearchContext context = new Payment_transactionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_transaction> list = new ArrayList<Payment_transaction>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_transaction> clientModelList = payment_transactionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_transaction clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_transaction>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据付款令牌查询
     */
    @Override
    public Page<Payment_transaction> selectByOdoo_payment_token(Integer id){
        Payment_transactionSearchContext context = new Payment_transactionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_transaction> list = new ArrayList<Payment_transaction>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_transaction> clientModelList = payment_transactionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_transaction clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_transaction>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据国家/地区查询
     */
    @Override
    public Page<Payment_transaction> selectByOdoo_partner_country(Integer id){
        Payment_transactionSearchContext context = new Payment_transactionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_transaction> list = new ArrayList<Payment_transaction>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_transaction> clientModelList = payment_transactionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_transaction clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_transaction>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据币种查询
     */
    @Override
    public Page<Payment_transaction> selectByOdoo_currency(Integer id){
        Payment_transactionSearchContext context = new Payment_transactionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_transaction> list = new ArrayList<Payment_transaction>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_transaction> clientModelList = payment_transactionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_transaction clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_transaction>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Payment_transaction> selectByOdoo_partner(Integer id){
        Payment_transactionSearchContext context = new Payment_transactionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_transaction> list = new ArrayList<Payment_transaction>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_transaction> clientModelList = payment_transactionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_transaction clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_transaction>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Payment_transaction> selectByOdoo_create(Integer id){
        Payment_transactionSearchContext context = new Payment_transactionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_transaction> list = new ArrayList<Payment_transaction>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_transaction> clientModelList = payment_transactionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_transaction clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_transaction>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Payment_transaction> selectByOdoo_write(Integer id){
        Payment_transactionSearchContext context = new Payment_transactionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_transaction> list = new ArrayList<Payment_transaction>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_transaction> clientModelList = payment_transactionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_transaction clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_transaction>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ipayment_transaction convert2Model(Payment_transaction domain , Ipayment_transaction model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = payment_transactionClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getAmountDirtyFlag())
            model.setAmount(domain.getAmount());
         if(domain.getPartner_phoneDirtyFlag())
            model.setPartner_phone(domain.getPartner_phone());
         if(domain.getPartner_emailDirtyFlag())
            model.setPartner_email(domain.getPartner_email());
         if(domain.getCallback_methodDirtyFlag())
            model.setCallback_method(domain.getCallback_method());
         if(domain.getReturn_urlDirtyFlag())
            model.setReturn_url(domain.getReturn_url());
         if(domain.getSale_order_ids_nbrDirtyFlag())
            model.setSale_order_ids_nbr(domain.getSale_order_ids_nbr());
         if(domain.getAcquirer_referenceDirtyFlag())
            model.setAcquirer_reference(domain.getAcquirer_reference());
         if(domain.getPartner_langDirtyFlag())
            model.setPartner_lang(domain.getPartner_lang());
         if(domain.getPartner_zipDirtyFlag())
            model.setPartner_zip(domain.getPartner_zip());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getDateDirtyFlag())
            model.setDate(domain.getDate());
         if(domain.getCallback_model_idDirtyFlag())
            model.setCallback_model_id(domain.getCallback_model_id());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getInvoice_idsDirtyFlag())
            model.setInvoice_ids(domain.getInvoice_ids());
         if(domain.getSale_order_idsDirtyFlag())
            model.setSale_order_ids(domain.getSale_order_ids());
         if(domain.getFeesDirtyFlag())
            model.setFees(domain.getFees());
         if(domain.getState_messageDirtyFlag())
            model.setState_message(domain.getState_message());
         if(domain.getCallback_res_idDirtyFlag())
            model.setCallback_res_id(domain.getCallback_res_id());
         if(domain.getPartner_cityDirtyFlag())
            model.setPartner_city(domain.getPartner_city());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getInvoice_ids_nbrDirtyFlag())
            model.setInvoice_ids_nbr(domain.getInvoice_ids_nbr());
         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.getHtml_3dsDirtyFlag())
            model.setHtml_3ds(domain.getHtml_3ds());
         if(domain.getPartner_nameDirtyFlag())
            model.setPartner_name(domain.getPartner_name());
         if(domain.getReferenceDirtyFlag())
            model.setReference(domain.getReference());
         if(domain.getPartner_addressDirtyFlag())
            model.setPartner_address(domain.getPartner_address());
         if(domain.getIs_processedDirtyFlag())
            model.setIs_processed(domain.getIs_processed());
         if(domain.getCallback_hashDirtyFlag())
            model.setCallback_hash(domain.getCallback_hash());
         if(domain.getCurrency_id_textDirtyFlag())
            model.setCurrency_id_text(domain.getCurrency_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getPayment_id_textDirtyFlag())
            model.setPayment_id_text(domain.getPayment_id_text());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getProviderDirtyFlag())
            model.setProvider(domain.getProvider());
         if(domain.getAcquirer_id_textDirtyFlag())
            model.setAcquirer_id_text(domain.getAcquirer_id_text());
         if(domain.getPartner_country_id_textDirtyFlag())
            model.setPartner_country_id_text(domain.getPartner_country_id_text());
         if(domain.getPayment_token_id_textDirtyFlag())
            model.setPayment_token_id_text(domain.getPayment_token_id_text());
         if(domain.getCurrency_idDirtyFlag())
            model.setCurrency_id(domain.getCurrency_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getPartner_country_idDirtyFlag())
            model.setPartner_country_id(domain.getPartner_country_id());
         if(domain.getPayment_token_idDirtyFlag())
            model.setPayment_token_id(domain.getPayment_token_id());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getPayment_idDirtyFlag())
            model.setPayment_id(domain.getPayment_id());
         if(domain.getAcquirer_idDirtyFlag())
            model.setAcquirer_id(domain.getAcquirer_id());
        return model ;
    }

    public Payment_transaction convert2Domain( Ipayment_transaction model ,Payment_transaction domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Payment_transaction();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getAmountDirtyFlag())
            domain.setAmount(model.getAmount());
        if(model.getPartner_phoneDirtyFlag())
            domain.setPartner_phone(model.getPartner_phone());
        if(model.getPartner_emailDirtyFlag())
            domain.setPartner_email(model.getPartner_email());
        if(model.getCallback_methodDirtyFlag())
            domain.setCallback_method(model.getCallback_method());
        if(model.getReturn_urlDirtyFlag())
            domain.setReturn_url(model.getReturn_url());
        if(model.getSale_order_ids_nbrDirtyFlag())
            domain.setSale_order_ids_nbr(model.getSale_order_ids_nbr());
        if(model.getAcquirer_referenceDirtyFlag())
            domain.setAcquirer_reference(model.getAcquirer_reference());
        if(model.getPartner_langDirtyFlag())
            domain.setPartner_lang(model.getPartner_lang());
        if(model.getPartner_zipDirtyFlag())
            domain.setPartner_zip(model.getPartner_zip());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getDateDirtyFlag())
            domain.setDate(model.getDate());
        if(model.getCallback_model_idDirtyFlag())
            domain.setCallback_model_id(model.getCallback_model_id());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getInvoice_idsDirtyFlag())
            domain.setInvoice_ids(model.getInvoice_ids());
        if(model.getSale_order_idsDirtyFlag())
            domain.setSale_order_ids(model.getSale_order_ids());
        if(model.getFeesDirtyFlag())
            domain.setFees(model.getFees());
        if(model.getState_messageDirtyFlag())
            domain.setState_message(model.getState_message());
        if(model.getCallback_res_idDirtyFlag())
            domain.setCallback_res_id(model.getCallback_res_id());
        if(model.getPartner_cityDirtyFlag())
            domain.setPartner_city(model.getPartner_city());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getInvoice_ids_nbrDirtyFlag())
            domain.setInvoice_ids_nbr(model.getInvoice_ids_nbr());
        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.getHtml_3dsDirtyFlag())
            domain.setHtml_3ds(model.getHtml_3ds());
        if(model.getPartner_nameDirtyFlag())
            domain.setPartner_name(model.getPartner_name());
        if(model.getReferenceDirtyFlag())
            domain.setReference(model.getReference());
        if(model.getPartner_addressDirtyFlag())
            domain.setPartner_address(model.getPartner_address());
        if(model.getIs_processedDirtyFlag())
            domain.setIs_processed(model.getIs_processed());
        if(model.getCallback_hashDirtyFlag())
            domain.setCallback_hash(model.getCallback_hash());
        if(model.getCurrency_id_textDirtyFlag())
            domain.setCurrency_id_text(model.getCurrency_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getPayment_id_textDirtyFlag())
            domain.setPayment_id_text(model.getPayment_id_text());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getProviderDirtyFlag())
            domain.setProvider(model.getProvider());
        if(model.getAcquirer_id_textDirtyFlag())
            domain.setAcquirer_id_text(model.getAcquirer_id_text());
        if(model.getPartner_country_id_textDirtyFlag())
            domain.setPartner_country_id_text(model.getPartner_country_id_text());
        if(model.getPayment_token_id_textDirtyFlag())
            domain.setPayment_token_id_text(model.getPayment_token_id_text());
        if(model.getCurrency_idDirtyFlag())
            domain.setCurrency_id(model.getCurrency_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getPartner_country_idDirtyFlag())
            domain.setPartner_country_id(model.getPartner_country_id());
        if(model.getPayment_token_idDirtyFlag())
            domain.setPayment_token_id(model.getPayment_token_id());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getPayment_idDirtyFlag())
            domain.setPayment_id(model.getPayment_id());
        if(model.getAcquirer_idDirtyFlag())
            domain.setAcquirer_id(model.getAcquirer_id());
        return domain ;
    }


}
